﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace OMXTreeViewNodes
{
    /// <summary>
    /// Auxiliary form for selecting and editting OMX structures and subsections
    /// </summary>
    public partial class ParamConfigSubSectionOps : Form
    {
        /// <summary>
        /// Possible modes of operation of the auxiliary form
        /// </summary>
        public enum OperationModes
        {
            /// <summary>
            /// Section reordering mode
            /// </summary>
            SectionReordering,
            /// <summary>
            /// Section selection mode
            /// </summary>
            SectionSelection
        }

        OperationModes mods;
        OMXParamTreeViewSectionNode[] subSecs;
        /// <summary>
        /// Public constructor
        /// </summary>
        /// <param name="iSubSecs">The OMX structures and subsections that should be displayed</param>
        /// <param name="iMods">Required interface mode of the form</param>
        public ParamConfigSubSectionOps(OMXParamTreeViewSectionNode[] iSubSecs, OperationModes iMods)
        {
            mods = iMods;

            InitializeComponent();
            
            switch (mods)
            {
                case OperationModes.SectionReordering:
                    Text = "Reorder available subsections";
                    break;
                case OperationModes.SectionSelection:
                    Text = "Select subsections";
                    btnMoveDown.Visible = false;
                    btnMoveUp.Visible = false;
                    btnApply.Text = "Ok";
                    break;
            }

            lbSectionList.DisplayMember = "OMXParamSectionName";

            for (int i = 0; i < iSubSecs.Length; i++)
            {
                lbSectionList.Items.Add(iSubSecs[i]);
            }
        }
        /// <summary>
        /// Returns the user selected nodes in an array
        /// </summary>
        /// <returns>Array of OMXParamTreeViewSectionNode nodes</returns>
        public OMXParamTreeViewSectionNode[] GetSelectedNodeArray()
        {
            return subSecs;
        }

        private void btnMoveUp_Click(object sender, EventArgs e)
        {
            if (lbSectionList.SelectedIndices.Count > 0)
            {

                int[] indxs_sel = new int[lbSectionList.SelectedIndices.Count];
                lbSectionList.SelectedIndices.CopyTo(indxs_sel, 0);
                Object[] objs_sel = new Object[lbSectionList.SelectedItems.Count];
                lbSectionList.SelectedItems.CopyTo(objs_sel, 0);

                for (int i = 0; i < indxs_sel.Length; i++)
                {
                    if (indxs_sel[0] > 0)
                    {
                        Object objTmp = objs_sel[i];
                        int crn_idx = indxs_sel[i] - 1;
                        lbSectionList.Items.Remove(objTmp);
                        lbSectionList.Items.Insert(crn_idx, objTmp);

                        lbSectionList.SelectedIndices.Add(crn_idx);
                    }
                }
            }
        }

        private void btnMoveDown_Click(object sender, EventArgs e)
        {
            if (lbSectionList.SelectedIndices.Count > 0)
            {

                int[] indxs_sel = new int[lbSectionList.SelectedIndices.Count];
                lbSectionList.SelectedIndices.CopyTo(indxs_sel, 0);
                Object[] objs_sel = new Object[lbSectionList.SelectedItems.Count];
                lbSectionList.SelectedItems.CopyTo(objs_sel, 0);

                for (int i = indxs_sel.Length - 1; i >= 0; i--)
                {
                    if (indxs_sel[indxs_sel.Length - 1] < lbSectionList.Items.Count - 1)
                    {
                        Object objTmp = objs_sel[i];
                        int crn_idx = indxs_sel[i] + 1;
                        lbSectionList.Items.Remove(objTmp);
                        lbSectionList.Items.Insert(crn_idx, objTmp);

                        lbSectionList.SelectedIndices.Add(crn_idx);
                    }
                }
            }
        }

        private void btnApply_Click(object sender, EventArgs e)
        {
            switch (mods)
            {
                case OperationModes.SectionReordering:
                    subSecs = new OMXParamTreeViewSectionNode[lbSectionList.Items.Count];
                    for (int i = 0; i < lbSectionList.Items.Count; i++)
                    {
                        subSecs[i] = (OMXParamTreeViewSectionNode)lbSectionList.Items[i];
                    }
                    break;
                case OperationModes.SectionSelection:
                    subSecs = new OMXParamTreeViewSectionNode[lbSectionList.SelectedItems.Count];
                    for (int i = 0; i < lbSectionList.SelectedItems.Count; i++)
                    {
                        subSecs[i] = (OMXParamTreeViewSectionNode)lbSectionList.SelectedItems[i];
                    }
                    break;
            }
            DialogResult = DialogResult.OK;
        }
    }
}
